﻿using EMoney.IStock.Client.Business;
using EMoney.IStock.Client.Business.HD;
using EMoney.IStock.Client.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EMoney.IStock.Client.Web.Controllers
{
    public class HDController : Controller
    {
        // 互动
        // GET: /HD/

        /// <summary>
        /// 问股（互动）
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            int userid = 0;
            if (!string.IsNullOrEmpty(Request["token"]))    //token有值时，执行认证操作
            {
                userid = TokenBll.GetUserIDByToken(Request["token"]);
            }
            else
            {
                userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            }

            if (userid == 0)
            {
                return null;
            }

            string css = StringHelper.GetStaticFile("IStockClientStatic/hd/css/main.css");

            if (!string.IsNullOrEmpty(Request["color"]) && Request["color"] == "black")
            {
                css = StringHelper.GetStaticFile("IStockClientStatic/hd/css/main1.css");
                ViewBag.Color = "black";
            }
            else
            {
                ViewBag.Color = "default";
            }

            ViewBag.PageCss = css;

            ViewBag.CIU = Request["channel"];   //频道编号"1401006";//
            ViewBag.UserID = userid;

            return View();
        }

        /// <summary>
        /// 提问
        /// </summary>
        /// <returns></returns>
        public string Ask()
        {
            int channelID = 0;
            string reqTitle = Request["title"];
            string reqContent = Request["content"];
            string reqChannelID = Request["cid"];

            if (string.IsNullOrEmpty(reqContent) || string.IsNullOrEmpty(reqChannelID))
            {
                return string.Empty;
            }

            if (!int.TryParse(reqChannelID, out channelID))
            {
                return string.Empty;
            }

            if (!string.IsNullOrEmpty(reqTitle))
            {
                reqTitle = Server.UrlDecode(reqTitle);
            }

            reqContent = Server.UrlDecode(reqContent);

            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }

            HDBll hdBll = new HDBll();

            if (hdBll.AddTopic(reqTitle, reqContent, userid, channelID))
            {
                return "1";
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取最近问答
        /// </summary>
        /// <returns></returns>
        public string GetRecentReply()
        {
            int channelID = 0;
            string strChannelID = Request["cid"];   //频道编号

            if (string.IsNullOrEmpty(strChannelID))
            {
                return string.Empty;
            }

            if (!int.TryParse(strChannelID, out channelID))
            {
                return string.Empty;
            }

            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }

            HDBll hdBll = new HDBll();

            return hdBll.RecentReply(userid, channelID);
        }

        /// <summary>
        /// 获取我的问答
        /// </summary>
        /// <returns></returns>
        public string GetMyTopic()
        {
            int channelID = 0;
            string strChannelID = Request["cid"];   //频道编号

            if (string.IsNullOrEmpty(strChannelID))
            {
                return string.Empty;
            }

            if (!int.TryParse(strChannelID, out channelID))
            {
                return string.Empty;
            }

            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }

            HDBll hdBll = new HDBll();

            return hdBll.MyTopic(userid, channelID);
        }

        /// <summary>
        /// 投票
        /// </summary>
        /// <returns></returns>
        public string Vote()
        {
            int tid = 0;
            int rid = 0;
            int flag = 0;
            string strReqTID = Request["tid"];    //提问编号
            string strReqRID = Request["rid"];    //回复编号
            string strReqFlag = Request["flag"];

            if (string.IsNullOrEmpty(strReqTID) || string.IsNullOrEmpty(strReqRID) || string.IsNullOrEmpty(strReqFlag))
            {
                return string.Empty;
            }

            if (!int.TryParse(strReqTID, out tid))
            {
                return string.Empty;
            }
            if (!int.TryParse(strReqRID, out rid))
            {
                return string.Empty;
            }

            if (!int.TryParse(strReqFlag, out flag))
            {
                return string.Empty;
            }
            else if (flag != 0 && flag != 1)
            {
                return string.Empty;
            }

            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }

            HDBll hdBll = new HDBll();

            return hdBll.Vote(userid, tid, rid, flag);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public string Del()
        {
            int tid = 0;
            int cid = 0;
            string strReqTID = Request["tid"];    //提问编号
            string strReqCID = Request["cid"];    //频道编号

            if (string.IsNullOrEmpty(strReqTID) || string.IsNullOrEmpty(strReqCID))
            {
                return string.Empty;
            }

            if (!int.TryParse(strReqTID, out tid))
            {
                return string.Empty;
            }

            if (!int.TryParse(strReqCID, out cid))
            {
                return string.Empty;
            }

            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }

            return new HDBll().Delete(userid, tid, cid);
        }
    }
}
